package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    private ITravelCommendService travelCommendService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        model.addAttribute("pageInfo",travelCommendService.query(qo));
        return "/travelCommend/list";
    }
    @RequestMapping("/getById")
    @ResponseBody
    public TravelCommend getById(Long id){
        return travelCommendService.getById(id);
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(MultipartFile file, TravelCommend entity) {
        //如果有文件,就上传
        if(file!=null&&file.getSize()>0){
            String url = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            entity.setCoverUrl(url);
        }
        travelCommendService.saveOrUpdate(entity);
        return new JSONResult(); //象征性返回操作成功
    }
}
